package com.heima.home.mapper;

import com.heima.home.dto.BookingListItemDTO;
import com.heima.home.entity.Bookings;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface BookingMapper {
    @Select("select * from bookings where id=#{id}")
    Bookings selectBookingById(Integer id);

    @Select("SELECT " +
            "b.id, b.booking_no, b.check_in_date, b.check_out_date, b.total_amount, b.status, b.payment_status, b.created_at, " +
            "p.id as property_id, p.title as property_title, p.image_url as property_image_url, p.city as property_city " +
            "FROM bookings b " +
            "JOIN properties p ON b.property_id = p.id " +
            "WHERE b.user_id = #{userId} " +
            "ORDER BY b.created_at DESC")
    @Results({
            @Result(column = "booking_no", property = "bookingNo"),
            @Result(column = "check_in_date", property = "checkInDate"),
            @Result(column = "check_out_date", property = "checkOutDate"),
            @Result(column = "total_amount", property = "totalAmount"),
            @Result(column = "payment_status", property = "paymentStatus"),
            @Result(column = "created_at", property = "createdAt"),
            @Result(column = "property_id", property = "propertyId"),
            @Result(column = "property_title", property = "propertyTitle"),
            @Result(column = "property_image_url", property = "propertyImageUrl"),
            @Result(column = "property_city", property = "propertyCity")
    })
    List<BookingListItemDTO> findByUserId(Integer userId);
    
    @Select("select count(*) from bookings where user_id = #{userId}")
    Integer countByUserId(Integer userId);
}
